package com.demo.page9101.controller.columnManagement;

import com.demo.common.entity.ColumnManagement;
import com.demo.common.entity.Meta;
import com.demo.common.entity.News;
import com.demo.page9101.feign.columnManagement.ColumnManagementFeign;
import com.demo.page9101.feign.news.NewsFeign;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 查询父节点、父类下的子节点
 * 作者：孙佳佳、张兴然
 * 补充：张佑威
 * 分配任务：张改
 * 插入数据：贾冠博
 * 接口文档：贾冠博
 */
@RestController
@CrossOrigin
public class ColumnManagementController {
    @Resource
    private ColumnManagementFeign columnManagementFeign;

    @Resource
    private NewsFeign newsFeign;

    //    查询所有父节点
    @GetMapping("/ColumnManagement/getfather")
    public List<ColumnManagement> fatherManagerMent(){
        System.out.println(columnManagementFeign.fatherAll());
        return columnManagementFeign.fatherAll();
    }

    //    查询单个类别下所有子类
    @GetMapping("/ColumnManagement/gettype")
    public Map mangerMentMap(@RequestParam(value = "id",required = true) Integer id){
        System.out.println(columnManagementFeign.typeAll(id));
        Map<String, Object> meta = new HashMap<>();
        HashMap<String, Object> dates = new HashMap<>();
        List<ColumnManagement> typemanagers = columnManagementFeign.typeAll(id);
        dates.put("typemanagers",typemanagers);
        Map<String, Object> map = new HashMap<>();
        System.out.println("id = " + id);
        for (ColumnManagement typemanager : typemanagers) {

            if (typemanager.getColumnFatherId()==id) {
                System.out.println("typemanager.getColumnId() = " + typemanager.getColumnId());
                List<News> list = newsFeign.newsproduct(typemanager.getColumnId(), -1);
                String columnName = typemanager.getColumnName();
                System.out.println("list = " + list);
                if (list.size()>0){
                    map.put(columnName,list);
                }
            }
        }
        dates.put("news",map);
        meta.put("date", dates);
        meta.put("meta", new Meta("获取成功", "200"));
        return  meta;

    }



}
